package com.fairytask.items
{
	import com.fairytask.utils.Vec2D;
	
	import flash.media.SoundTransform;
	
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.textures.Texture;
	
	public class Cell extends Sprite
	{
		internal var _cell:Image;
		
		internal var cellSndTrans:SoundTransform;
		
		internal var _showUnits:Boolean=true;
		
		internal var _maxUnits:uint=0;
		
		internal var _scale:Number;
		
		internal var _unitValue:Number=1;
		
		internal var _isStart:Boolean=false;
		
		public var vel:Number=0;
		
		internal var cellSnd:flash.media.Sound;
		
		internal var _radius:Number=0;
		
		internal var _totalTime:int=0;
		
		public var ov:Vec2D;
		
		internal var _numUnits:Number=0;
		
		internal var _txt:MovieClip;
		
		internal var _curTime:int=0;
		
		internal var _lastTime:int=0;
		
		internal var _unitType:int=-1;
		
		internal var friction:Number=0.97;
		public function Cell(pTexture:Texture)
		{
			this.ov = new Vec2D();
			this.cellSndTrans = new SoundTransform(0.1);
			super();
			this._cell = new Image(pTexture);
			this._cell.rotation = Math.random() * 360;
			addChild(this._cell);
			/*this._txt = utils.SWFClass.get("CellTextMC");
			this._txt.text_txt.text = Math.round(this._numUnits).toString();
			this._txt.text_txt.mouseEnabled = false;
			addChild(this._txt);
			this.cellSnd = utils.SWFClass.get("CellSnd");*/
			this.radius = 100;
		}
		
		public function playScaleEffect():void
		{
			//caurina.transitions.Tweener.removeTweens(this._cell);
			caurina.transitions.Tweener.addTween(this._cell, {"scaleX":this._scale + 0.1, "scaleY":this._scale + 0.1, "time":0.1, "transition":"linear", "onComplete":this.scaleBack});
		/*	if (Main.SOUND) 
			{
				this.cellSnd.play(0, 0, this.cellSndTrans);
			}
			return;*/
		}
		
		public function init(arg1:Number, arg2:Number):void
		{
			this._scale = arg1;
			this._cell.scaleX = arg1;
			this._cell.scaleY = arg2;
			this.radius = 30 * arg1;
			this._maxUnits = this._radius * 3;
			this._totalTime = Math.abs(1 - this._radius / 100) * 2500;
			return;
		}
		
		public function set numUnits(arg1:Number):void
		{
			var loc1:*=this._numUnits;
			this._numUnits = arg1;
			if (this._numUnits >= 300) 
			{
				this._numUnits = 300;
			}
			this._txt.text_txt.text = Math.round(this._numUnits).toString();
			if (this._numUnits > this._maxUnits) 
			{
				this._radius = this._numUnits / 3;
				caurina.transitions.Tweener.addTween(this._cell, {"scaleX":this._radius / 30, "scaleY":this._radius / 30, "time":0.2, "transition":"linear"});
			}
			else if (loc1 > this._maxUnits && this._numUnits < this._maxUnits && this._radius > this._maxUnits / 3) 
			{
				this._radius = this._maxUnits / 3;
				caurina.transitions.Tweener.addTween(this._cell, {"scaleX":this._radius / 30, "scaleY":this._radius / 30, "time":0.5, "transition":"linear"});
			}
			return;
		}
		
		public function get numUnits():Number
		{
			return this._numUnits;
		}
		
		public function get randVec():utils.Vec2D
		{
			var loc1:*=this._radius / 2;
			return new utils.Vec2D(x + (-loc1 + Math.random() * this._radius), y + (-loc1 + Math.random() * this._radius));
		}
		
		public function get vec():utils.Vec2D
		{
			return new utils.Vec2D(x, y);
		}
		
		public function set isStart(arg1:Boolean):void
		{
			this._isStart = arg1;
			return;
		}
		
		public function get radius():Number
		{
			return this._radius;
		}
		
		public function set radius(arg1:Number):void
		{
			this._radius = arg1;
			this._txt.text_txt.width = this._radius + this._radius;
			this._txt.text_txt.x = (-this._txt.width) / 2;
			return;
		}
		
		public function set unitType(arg1:int):void
		{
			this._unitType = arg1;
			var loc1:*=Game.getUnitType(this._unitType);
			if (!loc1) 
			{
				this._cell.inards_mc.alpha = 0.6;
				this._cell.nucleus_mc.alpha = 0.6;
				return;
			}
			caurina.transitions.Tweener.addTween(this._cell.inards_mc, {"_color":loc1.color, "alpha":1, "time":0.5, "transition":caurina.transitions.Equations.easeNone});
			caurina.transitions.Tweener.addTween(this._cell.nucleus_mc, {"_color":loc1.color, "alpha":1, "time":0.5, "transition":caurina.transitions.Equations.easeNone});
			this._unitValue = loc1.value;
			return;
		}
		
		public function updateMove(arg1:uint, arg2:Number):void
		{
			if (this.x + this._radius > Game.WIDTH) 
			{
				if (this.ov.x > 0) 
				{
					this.ov.x = -this.ov.x;
				}
				this.vel = (1 - (this.x + this._radius - Game.WIDTH) / this._radius) * 25 + 1;
			}
			else if (this.x - this._radius < 0) 
			{
				if (this.ov.x < 0) 
				{
					this.ov.x = Math.abs(this.ov.x);
				}
				this.vel = (1 - Math.abs(this.x - this._radius) / this._radius) * 25 + 1;
			}
			if (this.y + this._radius > Game.HEIGHT) 
			{
				if (this.ov.y > 0) 
				{
					this.ov.y = -this.ov.y;
				}
				this.vel = (1 - (this.y + this._radius - Game.HEIGHT) / this._radius) * 25 + 1;
			}
			else if (this.y - this._radius < 0) 
			{
				if (this.ov.y < 0) 
				{
					this.ov.y = Math.abs(this.ov.y);
				}
				this.vel = (1 - Math.abs(this.y - this._radius) / this._radius) * 25 + 1;
			}
			if (this.vel > 1) 
			{
				this.vel = this.vel * this.friction;
			}
			else 
			{
				this.vel = 0;
			}
			var loc1:*=this.ov.mulNum(this.vel);
			this.x = this.x + loc1.x * arg2;
			this.y = this.y + loc1.y * arg2;
			if (this.unitType < 0) 
			{
				return;
			}
			this._curTime = arg1 - this._lastTime;
			this._totalTime = (1 - this._radius / 100) * 1000 * this._unitValue;
			if (this._curTime >= this._totalTime) 
			{
				this._lastTime = arg1;
				if (this._numUnits < this._maxUnits) 
				{
					var loc2:*;
					var loc3:*=((loc2 = this).numUnits + 1);
					loc2.numUnits = loc3;
				}
				else if (this._numUnits > this._maxUnits) 
				{
					loc3 = ((loc2 = this).numUnits - 1);
					loc2.numUnits = loc3;
				}
			}
			return;
		}
		
		public function scaleBack():void
		{
			caurina.transitions.Tweener.addTween(this._cell, {"scaleX":this._scale, "scaleY":this._scale, "time":0.5, "transition":"linear"});
			return;
		}
		
		public function get isStart():Boolean
		{
			return this._isStart;
		}
		
		public function get unitType():int
		{
			return this._unitType;
		}
		
		public function explode():void
		{
			caurina.transitions.Tweener.removeTweens(this);
			caurina.transitions.Tweener.addTween(this, {"alpha":0, "scaleX":0, "scaleY":0, "time":0.3, "transition":caurina.transitions.Equations.easeInBack});
			return;
		}
		
		public function set showUnits(arg1:Boolean):void
		{
			this._showUnits = arg1;
			this._txt.visible = this._showUnits;
			return;
		}
		
		public function get maxUnits():uint
		{
			return this._maxUnits;
		}
		
		public function get showUnits():Boolean
		{
			return this._showUnits;
		}
	}
}